import 'dart:io';

import 'package:chathouse/public/Config.dart';
import 'package:chathouse/util/log.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
// not in use
// import 'package:flutter_app_badger/flutter_app_badger.dart';

// APPLE
// import 'package:jpush_flutter/jpush_flutter.dart';

// GOOGLE
import 'package:jpush_google_flutter/jpush_google_flutter.dart';

import '../constant/app_constant.dart';

/// @class : chat_house_push
/// @date : 2023-03-24 15:16
/// @name : LFC
/// @description ：極光推送

class ChatHousePush {
  ChatHousePush._init();

  static final ChatHousePush _instance = ChatHousePush._init();

  static ChatHousePush get instance {
    return _instance;
  }

  final JPush jpush = JPush();

  Future<void> initPlatformState() async {
    try {
      LogUtil.d("jpushjpush: $jpush");

      jpush.setAuth(enable: true);
      jpush.setWakeEnable(enable: true);
      jpush.setup(
        // appKey: "da232faf9d36b9dda044dd33",//旧包的
        appKey: "3ab469730b6b70a4292fcf13",
        //TODO 你自己应用的 AppKey 這是我的dc760410afc4d0ce67380f6c
        channel: "chatHouse",
        production: false,
        debug: true,
      );

      jpush.addEventHandler(
          onReceiveNotification: (Map<String, dynamic> message) async {
        LogUtil.d("flutter ---->接收到推送 $message");
        int num = 0;
        try {
          num = message["num"];
          if (Platform.isAndroid) {
            _showBadge(num);
          } else {
            ChatHousePush.instance.jpush.setBadge(num);
          }
        } catch (e) {
          LogUtil.d("flutter 設置角標數量錯誤: $message");
        }
      }, onOpenNotification: (Map<String, dynamic> message) async {
        LogUtil.d("flutter onOpenNotification: $message");
      }, onReceiveMessage: (Map<String, dynamic> message) async {
        LogUtil.d("flutter onReceiveMessage: $message");
      }, onReceiveNotificationAuthorization:
              (Map<String, dynamic> message) async {
        LogUtil.d("flutter onReceiveNotificationAuthorization: $message");
      }, onNotifyMessageUnShow: (Map<String, dynamic> message) async {
        LogUtil.d("flutter onNotifyMessageUnShow: $message");
      }, onConnected: (Map<String, dynamic> message) async {
        LogUtil.d("flutter onConnected: $message");
      });
    } on PlatformException {
      LogUtil.d("---->获取平台版本失败");
    }

    jpush.applyPushAuthority(
        const NotificationSettingsIOS(sound: true, alert: true, badge: true));

    jpush.getRegistrationID().then((rid) {
      SpUtil.putString(AppConstant.jPushRegistrationID, rid);
      LogUtil.d("flutter JPush getRegistrationID : $rid");
    });
  }

  Future<Null> _showBadge(int num) async {
    const badgeChannel = MethodChannel("chathouse.flutter");
    try {
      final result = await badgeChannel.invokeMethod('getBadge', {
        "badgeNum": num,
      });
    } on PlatformException catch (e) {
      if (kDebugMode) {
        LogUtil.d("_showBadge error: ${e.message}");
      }
    }
    // just add code to try
    // if(num == 0){
    //   FlutterAppBadger.removeBadge();
    // }else{
    //   FlutterAppBadger.updateBadgeCount(num);
    // }
  }
}
